package com.gzdh.dishrecognition.ui.fragment.report;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.dhkj.web.bean.DiningTableListDto;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.adapter.report.DiningTableReportAdapter;
import com.gzdh.dishrecognition.ui.dialog.DiningTableDetailDialog;
import com.gzdh.dishrecognition.ui.dialog.PopupWindowDiningTable;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class DinnerTableReportFragment extends Fragment {

    private Context mContext;

    private Activity activity;

    private RecyclerView recyclerView;

    private DiningTableReportAdapter diningTableReportAdapter;

    private PopupWindowDiningTable popupWindow;

    private TextView totalCostTv;

    private TextView amountTv;

    private String nameEditText;

    private String bqh;

    private String ksh;

    private final Integer type;

    public DinnerTableReportFragment(Activity activity, Integer type) {
        this.activity = activity;
        this.type = type;
    }

    @SuppressLint("MissingInflatedId")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.fragment_report_dinner_table, container, false);
        mContext = view.getContext();
        bqh = "0000";
        ksh = "0000";
        view.findViewById(R.id.sxBut).setOnClickListener(view1 -> {
            if(popupWindow == null) {
                popupWindow = new PopupWindowDiningTable(activity, view1);
                popupWindow.setFocusable(true);
                popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.END | Gravity.BOTTOM, 0, 0);
                popupWindow.setUserSubmit(this::getOrder);
            } else {
                popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.END | Gravity.BOTTOM, 0, 0);
            }
            nameEditText = popupWindow.getName();
            bqh = popupWindow.getBqh();
            ksh = popupWindow.getKsh();
        });
        view.findViewById(R.id.reLoadBut).setOnClickListener(view1 -> {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            getOrder(simpleDateFormat.format(new Date()), simpleDateFormat.format(new Date()), nameEditText, bqh, ksh);
        });
        recyclerView = view.findViewById(R.id.recyclerView);
        totalCostTv = view.findViewById(R.id.totalCostTv);
        amountTv = view.findViewById(R.id.amountTv);
        diningTableReportAdapter = new DiningTableReportAdapter(activity);
        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(layoutManager);
        diningTableReportAdapter.setUserClick((ksh, dinner, dcTime, times) -> {
            RequestUtils.getDiningTableDetails(mContext, ksh, dinner, dcTime, times, new MyObserverRes<>(mContext) {
                @Override
                public void onSuccess(JSONArray result) {
                    DiningTableDetailDialog dialog = new DiningTableDetailDialog(mContext, type, ksh, dinner, dcTime);
                    dialog.show();
                    dialog.setJsonArray(result);
                    dialog.setFinalDispose(() -> {
                        dialog.dismiss();
                        onResume();
                    });
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });
        });
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        bqh = "0000";
        ksh = "0000";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        getOrder(simpleDateFormat.format(new Date()), simpleDateFormat.format(new Date()), nameEditText, bqh, ksh);
        popupWindow = null;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    public void getOrder(String startTime, String endTime, String name, String bqh, String ksh) {
        RequestUtils.getDiningTableList(mContext, type, startTime, endTime, bqh, ksh,
        new MyObserverRes<>(mContext) {
            @Override
            public void onSuccess(List<DiningTableListDto> result) {
                BigDecimal price = new BigDecimal(0);
                BigDecimal totalCost = new BigDecimal(0);
                for (DiningTableListDto goo : result) {
                    String je = goo.getPrice() != null ? goo.getPrice() : "0";
                    price = price.add(new BigDecimal(je));
                    String cost = goo.getTotalCost() != null ? goo.getTotalCost() : "0";
                    totalCost = totalCost.add(new BigDecimal(cost));
                }
                recyclerView.setAdapter(diningTableReportAdapter);
                diningTableReportAdapter.setReserveListDtoList(result);
                amountTv.setText(price.toPlainString());
                totalCostTv.setText(totalCost.toPlainString());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                if(e != null && e.getCause() != null) {
                    if(e.getCause().toString().contains("java.net.ConnectException")) {
                        errorMsg = "网络异常";
                    }
                }
                Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }


}
